define(['../deviceModule'], function(module) {
    module.controller("deviceDetailCtrl", ['$scope', '$rootScope', '$state', '$stateParams', '$sessionStorage','$timeout', 'httpService', 'ENV','growl', 'Upload', 'ngDialog', 'Md5', function($scope, $rootScope, $state, $stateParams, $sessionStorage, $timeout, httpService, ENV, growl, Upload, ngDialog, Md5) {
        'use strict';
        $scope.id = $stateParams.id;
        console.log($stateParams);
        httpService.linkHttp({
            url: ENV.deviceDetail
        }, {deviceId: $scope.id}).then(function(res) {
            // 判断请求成功
            var result = res.data;
            if (result.code == "000000") {
                $scope.addDevInfo = result.data.deviceDetail;
            }
        });



        $scope.editDevice = function(valid) {
            if (valid) {
                // 禁用按钮
                $scope.disableFlag = true;

                var data = {};
                data.simNo = $scope.addDevInfo.sim_no;
                data.IMEI = $scope.addDevInfo.imei;
                data.deviceStatus = $scope.addDevInfo.device_status;
                data.rightStatus = $scope.addDevInfo.right_status;
                data.location = $scope.addDevInfo.location;
                data.deviceId = $scope.id;

                httpService.linkHttp({
                    url: ENV.deviceEdit
                },data).then(function(res) {
                    // 判断请求成功
                    var result = res.data;
                    if (result.code == "000000") {
                        // 重置表单
                        $scope.addDevInfo = {};
                        $scope.date = {};
                        $timeout(function () {
                            $state.go('main.device');
                        }, 1000);
                        growl.addSuccessMessage("编辑成功,1秒后将跳转到设备列表页");
                    } else {
                        var errText;
                        result.errDesc ? errText = result.errDesc : errText = '编辑失败, 请稍后再尝试';
                        growl.addErrorMessage(errText);
                    }
                    $scope.disableFlag = false;
                });
            } else {
                $scope.chechErrorForm($scope.deviceDetilForm.$error);
            }
        }


    }])
});
